﻿using System;
using System.Linq.Expressions;
using YanZhiwei.DotNet4.PCL.Utilities.Base;
namespace YanZhiwei.DotNet4.PCL.Utilities.Common
{
    /// <summary>
    /// INotifyPropertyChanged 帮助累
    /// </summary>
    public static class IPropertyChangedHelper
    {
        /*
         * 参考：http://www.douban.com/note/90653980/
         * eg:
    public class Person : NotifyPropertyChanged
    {
        private string name;
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                if (name != value)
                {
                    name = value;
                    this.NotifyPropertyChanged(p => p.name);
                }
            }
        }
    }
        */
        #region 属性更改通知到客户端
        /// <summary>
        /// 属性更改通知到客户端
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <typeparam name="TProperty">泛型</typeparam>
        /// <param name="iPropertyChanged">PropertyChanged类型</param>
        /// <param name="expression">eg:this.NotifyPropertyChanged(p => p.Name);  </param>
        public static void NotifyPropertyChanged<T, TProperty>(this T iPropertyChanged,
          Expression<Func<T, TProperty>> expression) where T : NotifyPropertyChanged
        {
            MemberExpression _memberExpression = expression.Body as MemberExpression;
            if (_memberExpression != null)
            {
                string _propertyName = _memberExpression.Member.Name;
                iPropertyChanged.NotifyChanged(_propertyName);
            }
            else
            {
                throw new NotImplementedException();
            }
        }
        #endregion
    }
}
